package com.cj.yudada.scoring;

import com.cj.yudada.common.ErrorCode;
import com.cj.yudada.exception.BusinessException;
import com.cj.yudada.model.entity.App;
import com.cj.yudada.model.entity.UserAnswer;
import com.cj.yudada.model.enums.AppTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 * 根据注解  去执行
 * @author cc
 * @date 2024-11-10 19:58
 **/
@Service
public class ScoringStrategyExecutor {


    @Autowired
    private List<ScoringStrategy> scoringStrategyList;


    public UserAnswer doScore(List<String> choiceList, App app){
        AppTypeEnum appTypeEnum = AppTypeEnum.getEnumByValue(app.getAppType());


        if (appTypeEnum == null){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"应用配置有误，没有找到对应的策略");
        }


        for (ScoringStrategy strategy : scoringStrategyList) {
//            if(strategy.getClass().getAnnotation())
            if (strategy.getClass().isAnnotationPresent(ScoringStrategyConfig.class)){
                // 存在这个注解
                ScoringStrategyConfig scoringStrategyConfig = strategy.getClass().getAnnotation(ScoringStrategyConfig.class);
                if(scoringStrategyConfig.appType() == app.getAppType() && scoringStrategyConfig.scoringStrategy() == app.getScoringStrategy()){
                   return  strategy.doScore(choiceList,app);
                }
            }

        }

        throw new BusinessException(ErrorCode.SYSTEM_ERROR,"应用配置有误，没有找到对应的策略");
    }
}
